home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-23 | 3.9 KB | 189 lines | [TEXT/CWIE] |
- /*
- * AZN_TStr255.cp
- *
- * Wrapper class for Pascal Str255
- *
- * © Andrew Nemeth Warrimoo Australia 1995
- * aznemeng@zeta.org.au
- *
- * File created: 5 Jun 95
- * Modified: 5, 28, 29 Jun;
- * 2, 23 Oct 95.
- */
-
- /*
- Some notes about this class
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^
- It is meant to be a 'drop-in' replacement for the pascal Str255 string type.
- The idea being, where ever you would have used a Str255, you can now use this
- 'TStr255' class, with its overloaded operators to make your life easier.
-
- Thus to create & initialise string(s):
-
- TStr255 myString;
- TStr255 myOtherString = "\pInit. with me!";
- TStr255 myLastString = myOtherString;
- char txtMine[ 32 ] = { "\0" };
-
- To 'copy' string(s) (assignment):
-
- myString = "\pNew contents!";
- myOtherString = myString;
- // note, can assign C Strings!
- myLastString = "Can even copy C Strings!";
- // can assign ONTO C Strings!
- myString->copy2C( txtMine );
-
- To concat string(s):
-
- myString += "\p add me to end!";
- myOtherString += myString;
- myLastString += " can even concat C Strings!";
-
- myString->cat2C( txtMine );
-
- Because of the overloaded 'StringPtr' operator, can pass objects of this class to
- functions which expect 'StringPtr' parameters!
-
- myString = "\p12";
- // toolbox call
- ::StringToNum( myString, &lgNumber );
- ::EqualString( "\p12", myString, TRUE, TRUE );
-
- Finally, can access elements of the string array with the
- supplied [] operator, as well as grabbing the size of the
- 'string' by use of the * operator (just as you would for normal
- Str255's!)
-
- unsigned char chElement = myString[2];
- short shSize = *myString;
-
-
- PROBLEMS YOU SHOULD BE AWARE OF:
-
- o Typecasting to StringHandles etc. is going die;
- o Expect 'p2cStr' and the like to fail;
- o ptr manipulation is going to fail
- etc. myString += 10; chElement = *myString; <- this will always return size!
- */
-
-
- #include "AZN_TStr255.h" // Wrapper class for Str255
-
-
-
-
- TStr255 & TStr255::operator=( ConstStr255Param str255S )
- //
- // Assign Str255
- //
- {
- if ( ::EqualString( str255S, f_str255Item, TRUE, TRUE ) )
- return( *this );
-
- ::BlockMoveData( StringPtr(str255S), f_str255Item, str255S[0] + 1 );
-
- return( *this );
- }
-
-
-
- TStr255 & TStr255::operator+=( ConstStr255Param str255S )
- //
- // Concat Str255 onto class string,
- // truncating if new str > 255
- //
- {
- const short kshCpyChars = minNum( str255S[0], 255 - f_str255Item[0] );
-
-
- ::BlockMoveData( StringPtr(str255S) + 1, f_str255Item + (f_str255Item[0] + 1), long( kshCpyChars ) );
- f_str255Item[0] += kshCpyChars;
-
- return( *this );
- }
-
-
-
- TStr255 & TStr255::operator=( const char * txtC )
- //
- // Assign C string
- // truncating if str > 255
- //
- {
- const short kshCpyChars = minNum( short( myStrlen( (char *)txtC ) ), 255 );
-
-
- ::BlockMoveData( Ptr(txtC), f_str255Item + 1, long( kshCpyChars ) );
- f_str255Item[0] = kshCpyChars;
-
- return( *this );
- }
-
-
-
- TStr255 & TStr255::operator+=( const char * txtC )
- //
- // Concat C string onto class string,
- // truncating if new str > 255
- //
- {
- const short kshCpyChars = minNum( short( myStrlen( (char *)txtC ) ), 255 - f_str255Item[0] );
-
-
- ::BlockMoveData( Ptr(txtC), f_str255Item + (f_str255Item[0] + 1), long( kshCpyChars ) );
- f_str255Item[0] += kshCpyChars;
-
- return( *this );
- }
-
-
-
- void TStr255::copy2C( char * txtC )
- //
- // Given a C string, COPY the
- // class var 'Str255' onto it.
- //
- {
- ::BlockMoveData( f_str255Item + 1, txtC, long( f_str255Item[0] ) );
-
- txtC += f_str255Item[0];
- *txtC = '\0';
- }
-
-
-
- void TStr255::cat2C( char * txtC )
- //
- // Given a C string, CONCAT the
- // class var 'Str255' onto it.
- //
- {
- const long klgCLength = myStrlen( txtC );
-
-
- ::BlockMoveData( f_str255Item + 1, txtC + klgCLength, long( f_str255Item[0] ) );
-
- txtC += ( klgCLength + f_str255Item[0] );
- *txtC = '\0';
- }
-
-
-
- long TStr255::myStrlen( register char * txtItem )
- //
- // Replacement for ANSI '::strlen()'
- // (Hence no need to include ANSI libs in project)
- //
- {
- register long lgLength = 0L;
-
- if ( txtItem )
- {
- while ( *txtItem++ )
- lgLength++;
- }
-
- return ( lgLength );
- }
-